/***
 *文件上传.js
 *依赖 JQ cropper.js
 *
 */
(function(window){
    var FileUpLoad={};
    FileUpLoad.option=null;
    FileUpLoad.init=function (option) {
        FileUpLoad.option=option;
        option.container.cropper({
            aspectRatio:  3/4,
            crop: function(e) {
                var canvas= $('#image').cropper('getCroppedCanvas');
                var basec4=canvas.toDataURL();
                option.previewContainer.attr("src",basec4);
            }
        });

        option.leftBtn.click(function () {
            FileUpLoad.leftRotete();
        });
        option.rightBtn.click(function () {
            FileUpLoad.rightRotate();
        });

        option.subBtn.click(function () {
            FileUpLoad.submit(option.subUrl,option.subSuccessCallBack,option.subErrorCallBack);
        });
        FileUpLoad.bindInputFile();
        FileUpLoad.replaceUrl(option.initImage);
    }

    FileUpLoad.leftRotete=function () {
        FileUpLoad.option.container.cropper('rotate',45)
    }
    FileUpLoad.rightRotate=function () {
        FileUpLoad.option.container.cropper('rotate',-45)
    }
    FileUpLoad.replaceUrl=function (url) {
        FileUpLoad.option.container.cropper('replace',url)
    }

    FileUpLoad.bindInputFile=function () {
        $("#input-file-upload").change(function () {
            var bookfile=$("#input-file-upload")[0];
            for(i=0;i<bookfile.files.length;i++){
                var Freader=new FileReader();
                Freader.readAsDataURL(bookfile.files[i]);
                Freader.onload=function (res){
                    FileUpLoad.replaceUrl(this.result);
                }
            }
        })
    }

    FileUpLoad.submit=function (url,successCallBack,errorCallBack) {
        var canvas= FileUpLoad.option.container.cropper('getCroppedCanvas');
        var basec4=canvas.toDataURL();
        var images=dataURLtoBlob(basec4);
        var formData = new FormData(FileUpLoad.option.FormData);
        formData.append('files', images,FileUpLoad.option.fileName);
        $.ajax({
            url: url,
            type: 'POST',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            xhr: function(){
                var xhr = $.ajaxSettings.xhr();
                if(onprogress && xhr.upload) {
                    xhr.upload.addEventListener("progress" , onprogress, false);
                    return xhr;
                }
            }
        }).done(function(res) {
            successCallBack(res);
        }).fail(function(res) {
            errorCallBack(res);
        });
    }

    function dataURLtoBlob(dataurl) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while(n--){
            u8arr[n] = bstr.charCodeAt(n);
        }
        FileUpLoad.option.fileName="1."+mime.substring(mime.lastIndexOf("/")+1);
        console.log(FileUpLoad.option.fileName);
        return new Blob([u8arr], {type:mime});
    }

    function onprogress(evt){
        var loaded = evt.loaded;     //已经上传大小情况
        var tot = evt.total;      //附件总大小
        var per = Math.floor(100*loaded/tot);  //已经上传的百分比
        FileUpLoad.option.progress.attr("aria-valuenow",per);
        FileUpLoad.option.progress.width(per+"%");
        FileUpLoad.option.progress.text(per+"%");
    }
    window.FileUpLoad=FileUpLoad;
})(window)
